package com.utils;

import io.dronefleet.mavlink.MavlinkConnection;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
 * @className: DroneConnection
 * @description: 建立minihomer连接  懒汉式单例
 * @author: Kuangkeli
 * @date: 2024/07/01 11:24
 **/
@Component
public class DroneConnection {
    // 私有静态实例
    private static MavlinkConnection instance;
    private static String udpHost = "192.168.1.241";
    private static int udpPort = 8080 ;


    // 私有构造函数以防止直接实例化
    private void MavlinkConnection() {
    }

    // 公有的静态方法用于获取实例
    public static MavlinkConnection getInstance() throws SocketException {
        if (instance == null) {
            synchronized (DroneConnection.class) {
                if (instance == null) {
                    DatagramSocket socket = new DatagramSocket(udpPort);
                    InputStream in = new UDPInputStream(socket);
                    OutputStream out = new UDPOutputStream(socket, udpHost, udpPort);
                    instance = MavlinkConnection.create(in, out);
                }
            }
        }
        return instance;
    }

}



